﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.OleDb;
namespace WindowsFormsApplication3.Transaction
{
    class InvoiceTransaction
    {

        /// <summary>
        /// insert completed jobcode to tblcompletedjobecode
        /// </summary>
        /// <param name="invist"></param>
        /// <returns></returns>
        public Boolean insertcompletejobcode(ArrayList invist)
        {
            Boolean sucess=false;
             OleDbConnection oledbcon = new OleDbConnection(Program.ConnStr);
            oledbcon.Open();
            OleDbCommand cmd = new OleDbCommand("insert into tblcompletedjob_mastertable(jobpk ,customerpk ,completeddate )values(@pjobpk , @pcustomerpk,@completedate)", oledbcon);

           cmd.Parameters.AddWithValue("@pjobpk", invist[0]);
           cmd.Parameters.AddWithValue(" @pcustomerpk", invist[1]);
           cmd.Parameters.AddWithValue(" @completedate", invist[2]);
           cmd.ExecuteNonQuery();
             sucess=true;
           


           return sucess;

        }




        /// <summary>
        /// get a invoiceautically generated
        /// </summary>
        /// <returns></returns>
        
        public String newinvoiceget()
        {
            String invoiceno = null;
            invoiceno = "####";
           

                Int32 S = 0;
                //  lblinvoice.Text = "";
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
                oleDbConnection1.Open();


                String query = "SELECT TOP 1 invoicepk FROM invoice_mastertable ORDER BY invoicepk DESC";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, Program.ConnStr);


                DataSet ds = new DataSet();

                dAdapter.Fill(ds, "tblinvoicepk");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    S = 0;
                }
                else
                {
                    S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                }

                S++;

                invoiceno = "NINV#0" + S.ToString();

                //   lblinvoice.Text = "CINV#0" + S.ToString();
                //  lblinvoice.Enabled = false;

     



            return invoiceno;


        }

        /// <summary>
        /// insert detials into invoicemastertable
        /// </summary>
        /// <param name="invist"></param>
        /// <returns></returns>

        public Boolean insertintomasterinvoice(ArrayList invist)
        {
            OleDbTransaction mytrans;
            Boolean sucess = false;
            OleDbConnection oledbcon = new OleDbConnection(Program.ConnStr);
            oledbcon.Open();
            OleDbCommand cmd = new OleDbCommand("insert into invoice_mastertable (invoicenum ,invoicedate ,custpk ,paymentduedate ,salesman ,currency)values(@pinvoicenum , @pinvoicedate, @custpk, @paymentduedate,@psalesman,@pcurrency)", oledbcon);

            cmd.Parameters.AddWithValue("@pinvoicenum ", invist[0]);
            cmd.Parameters.AddWithValue(" @pinvoicedate", invist[1]);
            cmd.Parameters.AddWithValue("  @custpk", invist[2]);
            cmd.Parameters.AddWithValue("@paymentduedate", invist[3]);
            cmd.Parameters.AddWithValue("@psalesman", invist[4]);
            cmd.Parameters.AddWithValue("@pcurrency", invist[5]);
            mytrans = oledbcon.BeginTransaction();
            cmd.Transaction = mytrans;
            mytrans.Commit();
            cmd.ExecuteNonQuery();
           
            sucess = true;
            oledbcon.Close();


            
            return sucess;

        }



        /// <summary>
        /// invoice into invoicesubtable
        /// </summary>
        /// <param name="jobpk"></param>
        /// <param name="invoicepk"></param>
        /// <returns></returns>
        public Boolean insertintosubinvoice(int jobpk,int invoicepk)
        {
            Boolean sucess = false;
            
            OleDbConnection oledbcon = new OleDbConnection(Program.ConnStr);
            oledbcon.Open();
            OleDbCommand cmd = new OleDbCommand("insert into tbl_invoicesubtable (jobpk ,invoicepk )values(@pjobpk , @pinvoicepk)", oledbcon);

            cmd.Parameters.AddWithValue("@pjobpk ", jobpk);
            cmd.Parameters.AddWithValue(" @pinvoicepk", invoicepk);
            

            cmd.ExecuteNonQuery();
            sucess = true;



            return sucess;

        }



    }
}
